
<script>

import { initDict } from '@/api/dict'
import {StringToJson} from '@/utils/util'
import ExtTable from '@/components/lt/codeGenerator/components/ExtTable'
import defaultSettings from '@/config/defaultSettings'
import {SysMixin} from '@/mixins/SysMixin'
import {bus} from '@/utils/util'

export default {
    name:'LSelectTable',
    components:{ExtTable},
    mixins:[SysMixin],
    props:{
        value:{
            type:[String,Number,Array,],
        },
        form:{
            type:Object,
            default:()=>{}
        },
        disabled:{
            type:Boolean,
            default:false
        },
        initQuery:{
            type:[String,Array],
            default:()=>[]
        },
        initOrder:{
            type:[String,Array],
            default:()=>[]
        },
        axiosUrl:{
            type:String,
            required:false
        },
        field:String,
        rowKey:String,
        columns:{
             type:[String,Array],
            default:()=>[]
        },
        busEventName:String
    },
    watch:{
        

    },
    created() {
    },
    data(){
        return{
            StringToJson,
            //存放用户输入的值
            queue:[],
            scopedSlots:{},
            defaultSettings,
         
           
        }
    },
    methods: {
        //当输入信息发生变化
        onInputChange(e){
            let value
            if(e.target && e.target){
                value = e.target.value
            }else{
                value = e
            }
             let that = this
            if(that._.isEmpty(value)){
                return false
            }
            //将最新值放到第一位
            that.queue.unshift(value)
            that._.debounce(that.query,500)()
        },
        query(){
            if(this.queue.length==0){
                return false
            }

            let that = this
            let val = that.queue[0]
            that.queue=[]
            let loaclInitQuery = that._.cloneDeep(that.initQuery)
            loaclInitQuery.push({field:that.field,val:val,condition:'like'})
            that.$refs["selectTable"].setInitQuery(loaclInitQuery)
            that.$refs["selectTable"].searchQuery()
            //查询结束后，再调用一次查询，防止因为查询时间长，用户输入的值又发生变化了
            setTimeout(() => {
                that.query()
            }, 100);

        },
       //点击行事件，暂时没用
        customRow(record){
            let that = this
            return{
                on:{
                    click:(e)=>{
                        that.$bus.$emit(that.busEventName?that.busEventName:'onSelectTableClick', record)
                    }
                }
            }
        },
        formatColumns(){
            let columns = StringToJson(this.columns)
            for(let c of columns){
                if(c['dictCode']){
                     c['customRender']=(text)=>{
                        if(!text){
                            return ''
                        }else{
                            return this.dictText(this.defaultSettings.数据字典[c['dictCode']],text)
                        }
                    }
                }
            }
            return columns;
        },
    },
    
     render: function(h) {
        return (
             <a-dropdown trigger={['click']}>
                <a-input value={this.value}  onChange={this.onInputChange} disabled={this.disabled} allowClear={true}/>
                <a-menu slot="overlay" v-show={!this.disabled}>
                    <a-menu-item>
                        
                            {
                                h('ExtTable',{
                                    ref:'selectTable',
                                    props:{
                                        customRow:this.customRow,
                                        isAutoLoad:false,
                                        showPagination:false,
                                        columns:this.formatColumns(this.columns),
                                        rowKey:this.rowKey,
                                        axiosUrl:this.axiosUrl,
                                        initQuery:this.initQuery,
                                        initOrder:this.initOrder,
                                    }
                                })
                            }
                        
                    </a-menu-item>
                </a-menu>
             </a-dropdown>
        )
     }
}
</script>

